package com.rockcent.service.impl;

import com.rockcent.common.common.BusinessException;
import com.rockcent.helper.MerchantFundFlowHelper;
import com.rockcent.mall.lib.domain.Merchant;
import com.rockcent.mall.lib.domain.MerchantFund;
import com.rockcent.mall.lib.domain.MerchantFundFlow;
import com.rockcent.mall.lib.domain.MerchantRechargeFlow;
import com.rockcent.mall.lib.domain.enumclass.FundTransactionFlow;
import com.rockcent.mall.lib.domain.enumclass.MerchantFundFlowTransactionType;
import com.rockcent.mall.lib.domain.enumclass.MerchantPayType;
import com.rockcent.mall.lib.domain.enumclass.MerchantRechargeType;
import com.rockcent.mall.lib.repository.MerchantFundRepository;
import com.rockcent.mall.lib.repository.MerchantRepository;
import com.rockcent.service.MerchantFundFlowSrv;
import com.rockcent.service.MerchantFundSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by zhangjh on 2017/3/30.
 */
@Service
public class MerchantFundSrvImpl implements MerchantFundSrv {

    @Autowired
    private MerchantFundRepository merchantFundRepository;

    @Autowired
    private MerchantRepository merchantRepository;


    @Autowired
    private MerchantFundFlowSrv merchantFundFlowSrv;

    /**
     * 充值
     *
     * @param rechargeFlow
     */
    @Override
    public void recharge(MerchantRechargeFlow rechargeFlow) {


        if (null == rechargeFlow) {
            throw new BusinessException("信息错误");
        }

        Long merchantId = rechargeFlow.getMerchantId();
        Long mallId = rechargeFlow.getMallId();
        MerchantFundFlowTransactionType transactionType = MerchantFundFlowTransactionType.A_RECHARGE_INCOME;
        MerchantRechargeType rechargeType = rechargeFlow.getRechargeType();
        if (MerchantRechargeType.RECHARGE_PRESENTED == rechargeType) {
            transactionType = MerchantFundFlowTransactionType.A_RECHARGE_PRESENTED;
        }
//        MerchantFundFlowTransactionType transactionType = MerchantFundFlowTransactionType.A_RECHARGE_INCOME;
        FundTransactionFlow transactionFlow = FundTransactionFlow.INCOME;

        MerchantFundFlowHelper.SINGLETON.validateFundFlow(merchantId, mallId, transactionType, transactionFlow);


        Merchant merchant = merchantRepository.findOne(merchantId);

        Long merchantFundId = merchant.getMerchantFundId();
        MerchantFund merchantFund = merchantFundRepository.findOne(merchantFundId);

        BigDecimal operationAmount = rechargeFlow.getRechargeAmount();
        BigDecimal merchantFundTotalAmount = merchantFund.getTotalAmount();
        merchantFund.setTotalAmount(merchantFundTotalAmount.add(operationAmount));
        BigDecimal merchantFundAvailableAmount = merchantFund.getAvailableAmount();
        merchantFund.setAvailableAmount(merchantFundAvailableAmount.add(operationAmount));

        MerchantPayType payType = rechargeFlow.getPayType();
        //B主动充值
        if (MerchantPayType.ONLINE == payType || MerchantPayType.OUTLINE == payType) {
            merchantFund.setActualRechargeAmount(merchantFund.getActualRechargeAmount().add(operationAmount));
        }
        //M赠送
        else if (MerchantPayType.RECHARGE_MALL == payType && MerchantRechargeType.RECHARGE_PRESENTED == rechargeType) {
            merchantFund.setPresentRechargeAmount(merchantFund.getPresentRechargeAmount().add(operationAmount));
            //TODO 3.5.0 充值赠送不关联某一笔充值，M端的普通充值和赠送充值只是一个标记
            //TODO 3.6.0 充值赠送将关联某一笔充值
            merchantFund.setCanNotWithdrawAmount(merchantFund.getCanNotWithdrawAmount().add(operationAmount));
        } //M普通充值
        else if (MerchantPayType.RECHARGE_MALL == payType && MerchantRechargeType.RECHARGE_COMMON == rechargeType) {
            merchantFund.setActualRechargeAmount(merchantFund.getActualRechargeAmount().add(operationAmount));
        }

        merchantFund = merchantFundRepository.save(merchantFund);

        //资金流水表
        String seqNo = rechargeFlow.getSeqNo();
        String remark = rechargeFlow.getRemark();
        merchantFundFlowSrv.save(merchantFund, operationAmount, payType, seqNo, FundTransactionFlow.INCOME, transactionType,remark);


        //TODO 预留扣费，商户开通


    }


    /**
     * 扣费
     *
     * @param flow
     */
    @Override
    public void deduction(MerchantFundFlow flow) {

        if (null == flow) {
            throw new BusinessException("信息错误");
        }

        Long merchantId = flow.getMerchantId();
        Long mallId = flow.getMallId();
        MerchantFundFlowTransactionType transactionType = flow.getTransactionType();
        FundTransactionFlow transactionFlow = flow.getTransactionFlow();

        MerchantFundFlowHelper.SINGLETON.validateFundFlow(merchantId, mallId, transactionType, transactionFlow);


    }

    @Override
    public MerchantFund findOne(Long merchantId) {

        MerchantFund merchantFund = merchantFundRepository.findTop1ByMerchantId(merchantId);
        merchantFund = merchantFundRepository.findOne(merchantFund.getId());
        return merchantFund;
    }

    @Override
    public MerchantFund save(MerchantFund merchantFund) {
        return merchantFundRepository.save(merchantFund);
    }


}
